﻿#include "positionfeature2item.h"

#include <QDebug>
#include <QGraphicsScene>

PositionFeature2Item::PositionFeature2Item(const int level, const QStringList& machineList, const QList<QStringList>& valueList, QObject *parent, QGraphicsItem *item) : QObject(parent), QGraphicsItem(item),
    m_level(level),
    m_machineList(machineList),
    m_valueList(valueList)
{
    //缓存在设备坐标系中，并且只有在位置、大小或外观发生变化时才需要重新计算。
    this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}

QRectF PositionFeature2Item::boundingRect() const
{
    return QRectF(0, 0, scene()->width(), 200);
}

void PositionFeature2Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
    painter->setCompositionMode(QPainter::CompositionMode_Source);

    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->setFont(QFont("Helvetica", 12));

    // 绘制网格线
    painter->drawLine(QLineF(30, 0, 870, 0));
    painter->drawLine(QLineF(30, 40, 870, 40));
    for (int i = 0; i < 3; i++)
        painter->drawLine(QLineF(130, 80 + i * 40, 870, 80 + i * 40));
    painter->drawLine(QLineF(30, 200, 870, 200));

    painter->drawLine(QLineF(30, 0, 30, 200));
    painter->drawLine(QLineF(80, 40, 80, 200));
    painter->drawLine(QLineF(130, 40, 130, 200));
    for (int i = 0; i < 5; i++)
        painter->drawLine(QLineF(260 + i * 120, 0, 260 + i * 120, 200));
    painter->drawLine(QLineF(870, 0, 870, 200));

    // 文字说明
    painter->drawText(QPointF(100, 25), QStringLiteral("项目或参数"));
    painter->drawText(QPointF(290, 25), QStringLiteral("I作动器"));
    painter->drawText(QPointF(410, 25), QStringLiteral("II作动器"));
    painter->drawText(QPointF(530, 25), QStringLiteral("III作动器"));
    painter->drawText(QPointF(650, 25), QStringLiteral("IV作动器"));
    painter->drawText(QPointF(770, 25), QStringLiteral("技术要求"));
    painter->drawText(QPointF(45, 80), QStringLiteral("二"));
    painter->drawText(QPointF(45, 105), QStringLiteral("路"));
    painter->drawText(QPointF(45, 130), QStringLiteral("零"));
    painter->drawText(QPointF(45, 155), QStringLiteral("指"));
    painter->drawText(QPointF(45, 180), QStringLiteral("令"));
    painter->drawText(QPointF(95, 90), QStringLiteral("位"));
    painter->drawText(QPointF(95, 115), QStringLiteral("移"));
    painter->drawText(QPointF(95, 140), QStringLiteral("表"));
    painter->drawText(QPointF(95, 165), QStringLiteral("决"));
    painter->drawText(QPointF(140, 65), QStringLiteral("正最大摆角(°)"));
    painter->drawText(QPointF(140, 105), QStringLiteral("负最大摆角(°)"));
    painter->drawText(QPointF(140, 145), QStringLiteral("回环宽度δM(°)"));
    painter->drawText(QPointF(140, 185), QStringLiteral("线性度NL(%)"));
    // 技术要求分区一级/二级
    switch (m_level) {
    case 1:
        painter->drawText(QPointF(750, 65), QStringLiteral("≥10"));
        painter->drawText(QPointF(750, 105), QStringLiteral("≤-10"));
        painter->drawText(QPointF(750, 145), QStringLiteral("≤0.2"));
        painter->drawText(QPointF(750, 185), QStringLiteral("≤2"));
        break;
    case 2:
        painter->drawText(QPointF(750, 65), QStringLiteral("≥58.5"));
        painter->drawText(QPointF(750, 105), QStringLiteral("≤-58.5"));
        painter->drawText(QPointF(750, 145), QStringLiteral("≤3.5"));
        painter->drawText(QPointF(750, 185), QStringLiteral("≤3.5"));
        break;
    default:
        break;
    }

    // 文字数值: 1, 2, 3, 4
    if (m_machineList.contains("A1")) {
        painter->drawText(QPointF(290, 65), m_valueList.at(0).at(0));
        painter->drawText(QPointF(290, 105), m_valueList.at(1).at(0));
        painter->drawText(QPointF(290, 145), m_valueList.at(2).at(0));
        painter->drawText(QPointF(290, 185), m_valueList.at(3).at(0));
    }

    if (m_machineList.contains("A2")) {
        painter->drawText(QPointF(410, 65), m_valueList.at(0).at(1));
        painter->drawText(QPointF(410, 105), m_valueList.at(1).at(1));
        painter->drawText(QPointF(410, 145), m_valueList.at(2).at(1));
        painter->drawText(QPointF(410, 185), m_valueList.at(3).at(1));
    }

    if (m_machineList.contains("A3")) {
        painter->drawText(QPointF(530, 65), m_valueList.at(0).at(2));
        painter->drawText(QPointF(530, 105), m_valueList.at(1).at(2));
        painter->drawText(QPointF(530, 145), m_valueList.at(2).at(2));
        painter->drawText(QPointF(530, 185), m_valueList.at(3).at(2));
    }

    if (m_machineList.contains("A4")) {
        painter->drawText(QPointF(650, 65), m_valueList.at(0).at(3));
        painter->drawText(QPointF(650, 105), m_valueList.at(1).at(3));
        painter->drawText(QPointF(650, 145), m_valueList.at(2).at(3));
        painter->drawText(QPointF(650, 185), m_valueList.at(3).at(3));
    }
}
